var util = require('../../utils/util.js')
// var mymessage
Page({
  inputValue: '',
  data: {
    messageList: [],
    date: '',
  },
  bindinput(e) {
    // console.log(e);
    var mymessage = e.detail.value
    this.setData({
      mymessage: mymessage
    })
  },
  send(e) {
    var date = util.formatTime(new Date());
    // console.log(date);

    //把自己的消息加入消息列表
    let tempList = this.data.messageList
    let time = new Date().toLocaleDateString()
    var img = this.data.img
    var mymessage = this.data.mymessage

    tempList.push({
      content: mymessage,
      sender: 'me',
      time: time,
      img: img,
    })
    this.setData({
      inputValue: null,
      messageList: tempList,
      date: date
    })
    var that = this
    //把客服的消息加入消息列表

    // 判断  是否有关键字
    wx.request({
      url: 'http://localhost/shopping/chat_if.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        key: that.data.mymessage
      },
      success: function (res) {
        // console.log(res.data)
        if (res.data == '关键字已存在') {
          console.log('关键字已存在!!')
          wx.request({
            url: 'http://localhost/shopping/chat.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              key: that.data.mymessage
            },
            success: function (res) {
              console.log(res.data)
              //把服务端的消息加入消息列表
              let content = res.data.chat[0].answer
              let tempList = that.data.messageList
              let time = new Date().toLocaleDateString()
              wx.request({
                url: 'http://localhost/shopping/customer_add.php',
                header: {
                  'Content-Type': 'application/json'
                },
                data: {
                  userid: that.data.id,
                  usercontent: that.data.mymessage,
                  content: content,
                  date: date,
                },
                success: function (res) {
                  console.log(res.data)
                },
                fail: function (res) {
                  console.log("fail");
                }
              })
              tempList.push({
                content: content,
                sender: 'server',
                time: time,
                img: img
              })
              that.setData({
                messageList: tempList,
                date: date,
              })
            },
            fail: function (res) {
              console.log("fail");
            }
          })

        } else {
          console.log('关键字不存在!!')
          //智能机器人
          let url = 'https://api.jisuapi.com/iqa/query?appkey=9e1978e51b5fed68&question=' + mymessage;
          console.log(url);
          wx.request({
            url: url,
            success: res => {
              console.log('success', res);

              //把服务端的消息加入消息列表
              let content = res.data.result.content
              let tempList = that.data.messageList
              let time = new Date().toLocaleDateString()
              console.log(that.data.id, that.data.mymessage, content, date)
              wx.request({
                url: 'http://localhost/shopping/customer_add.php',
                header: {
                  'Content-Type': 'application/json'
                },
                data: {
                  userid: that.data.id,
                  usercontent: that.data.mymessage,
                  content: content,
                  date: date,
                },
                success: function (res) {
                  console.log(res.data)
                },
                fail: function (res) {
                  console.log("fail");
                }
              })

              tempList.push({
                content: content,
                sender: 'server',
                time: time,
                img: img
              })
              that.setData({
                messageList: tempList,
                date: date,
              })

            },
            fail: err => {
              console.log('fail', err);
            }
          })
        }

      },
      fail: function (res) {
        console.log("fail");
      }
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let name = getApp().globalData.usersname
    var that = this
    wx.request({
      url: 'http://localhost/shopping/users.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          img: res.data.users[0].img,
          id: res.data.users[0].id,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },





})